home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / lt_levelwidth.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  10KB  |  332 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #include "Assert.h"
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20. STATIC LONG __stdargs
  21. DefaultDispFunc(struct Gadget *gad,LONG value)
  22. {
  23.     return(value);
  24. }
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30. VOID
  31. LTP_LevelWidth(LayoutHandle *handle,STRPTR levelFormat,DISPFUNC dispFunc,LONG min,LONG max,LONG *maxWidth,LONG *maxLen,BOOL fullCheck)
  32. {
  33.     UBYTE buffer[80];
  34.     LONG localMaxWidth;
  35.     LONG localMaxLen;
  36.     LONG len;
  37.  
  38.     localMaxWidth    = 0;
  39.     localMaxLen    = 0;
  40.  
  41.     if(handle)
  42.     {
  43.         if(!dispFunc)
  44.             dispFunc = (DISPFUNC)DefaultDispFunc;
  45.  
  46.         if(levelFormat == NULL)
  47.             levelFormat = LTP_NumberFormat;    /* either %ld or %lD, according to locale */
  48.  
  49.         if(fullCheck)
  50.         {
  51.             LONG i;
  52.  
  53.             for(i = min ; i <= max ; i++)
  54.             {
  55.                 SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,i));
  56.  
  57.                 len = strlen(buffer);
  58.  
  59.                 if(len > localMaxLen)
  60.                     localMaxLen = len;
  61.  
  62.                 len = TextLength(&handle->RPort,buffer,len);
  63.  
  64.                 if(len > localMaxWidth)
  65.                     localMaxWidth = len;
  66.             }
  67.         }
  68.         else
  69.         {
  70.             SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,min));
  71.  
  72.             len = strlen(buffer);
  73.  
  74.             if(len > localMaxLen)
  75.                 localMaxLen = len;
  76.  
  77.             len = TextLength(&handle->RPort,buffer,len);
  78.  
  79.             if(len > localMaxWidth)
  80.                 localMaxWidth = len;
  81.  
  82.             SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,max));
  83.  
  84.             len = strlen(buffer);
  85.  
  86.             if(len > localMaxLen)
  87.                 localMaxLen = len;
  88.  
  89.             len = TextLength(&handle->RPort,buffer,len);
  90.  
  91.             if(len > localMaxWidth)
  92.                 localMaxWidth = len;
  93.         }
  94.     }
  95.  
  96.     if(maxLen)
  97.         *maxLen = localMaxLen;
  98.  
  99.     if(maxWidth)
  100.         *maxWidth = localMaxWidth + handle->GlyphWidth;
  101. }
  102.  
  103.  
  104. /*****************************************************************************/
  105.  
  106.  
  107. /****** gtlayout.library/LT_LevelWidth ******************************************
  108. *
  109. *   NAME
  110. *    LT_LevelWidth -- Determine the maximum width of a SLIDER_KIND
  111. *                     level string.
  112. *
  113. *   SYNOPSIS
  114. *    Index = LT_LevelWidth(Handle,FormatString,DispFunc,Min,Max,MaxWidth,
  115. *      D0                    A0        A1         A2     D0  D1    A3
  116. *
  117. *                              MaxLen,FullCheck);
  118. *                                A5      D2
  119. *
  120. *    LONG LT_LevelWidth(LayoutHandle *,STRPTR,
  121. *                       LONG (*)(struct Gadget *,WORD),LONG,LONG,LONG *,
  122. *                       LONG *,BOOL);
  123. *
  124. *   FUNCTION
  125. *    In order to make room for the level text displayed by a
  126. *    SLIDER_KIND object one needs to know how much space the
  127. *    longest level string will occupy. Otherwise, the level
  128. *    text may overwrite the gadget label text or the slider
  129. *    container. This routine will rattle through all possible
  130. *    slider settings (as given via the Min and the Max
  131. *    level values) and determine the longest label string
  132. *    according to the font used.
  133. *
  134. *   INPUTS
  135. *    Handle - Pointer to a LayoutHandle structure, as returned by
  136. *        a call to LT_CreateHandleTags().
  137. *
  138. *    FormatString - The sprintf() style formatting string to be used
  139. *        to format the slider level settings into text.
  140. *        This is the same string you would pass in via
  141. *        the GTSL_LevelFormat tag when creating the
  142. *        slider object.
  143. *        Default: "%lD" for systems which have locale.library
  144. *            installed, "%ld" otherwise.
  145. *
  146. *    DispFunc - A pointer to the function to filter the slider level
  147. *        values. The result of this function will then be
  148. *        used to format a string into the slider level text.
  149. *        This is the same parameter you would pass in via
  150. *        the GTSL_DispFunc tag when creating the slider
  151. *        object.
  152. *
  153. *            NOTE: the routine will be called with a NULL Gadget
  154. *                parameter, make sure your code will handle
  155. *                this nicely.
  156. *
  157. *        Default: no display function
  158. *
  159. *    Min - The smallest value the slider can be set to. This is
  160. *        same value you would pass in via GTSL_Min when creating
  161. *        the slider object.
  162. *
  163. *    Max - The largest value the slider can be set to. This is
  164. *        same value you would pass in via GTSL_Max when creating
  165. *        the slider object.
  166. *
  167. *    MaxWidth - Pointer to a place to store the width of the
  168. *        longest level string in pixels. If you pass
  169. *        in NULL instead of the address of a variable
  170. *        no harm will be done.
  171. *
  172. *    MaxLen - Pointer to a place to store the length of the
  173. *        longest level string in characters. If you pass
  174. *        in NULL instead of the address of a variable
  175. *        no harm will be done.
  176. *
  177. *    FullCheck - TRUE will cause the code to rattle through all
  178. *        possible slider settings, starting from the
  179. *        minimum value, ending at the maximum value.
  180. *        While this may be a good idea for a display
  181. *        function to map slider levels to text strings
  182. *        of varying length it might be a problem when
  183. *        it comes to display a range of numbers from
  184. *        1 to 40,000: the code will loop through
  185. *        40,000 iterations trying to find the longest
  186. *        string.
  187. *
  188. *        FALSE will cause the code to calculate the
  189. *        longest level string based only on the
  190. *        minimum and the maximum value to check.
  191. *        While this is certainly a good a idea when
  192. *        it comes to display a range of numbers from
  193. *        1 to 40,000 as only two values will be
  194. *        checked the code may fail to produce
  195. *        accurate results for sliders using display
  196. *        functions mapping slider levels to strings.
  197. *
  198. *   RESULT
  199. *    Index - The slider level which gives the longest
  200. *        level string.
  201. *
  202. *   NOTES
  203. *    Some compilers have trouble passing parameters in A5. In such
  204. *    a case it is recommended to use gtlayout.library/LT_NewLevelWidth
  205. *    instead.
  206. *
  207. *   SEE ALSO
  208. *    gtlayout.library/LT_NewLevelWidth
  209. *
  210. ******************************************************************************
  211. *
  212. */
  213.  
  214. VOID LIBENT
  215. LT_LevelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR levelFormat,REG(a2) DISPFUNC dispFunc,REG(d0) LONG min,REG(d1) LONG max,REG(a3) LONG *maxWidth,REG(a5) LONG *maxLen,REG(d2) BOOL fullCheck)
  216. {
  217.     LTP_LevelWidth(handle,levelFormat,dispFunc,min,max,maxWidth,maxLen,fullCheck);
  218. }
  219.  
  220. /****** gtlayout.library/LT_NewLevelWidth ******************************************
  221. *
  222. *   NAME
  223. *    LT_NewLevelWidth -- Determine the maximum width of a SLIDER_KIND
  224. *                        level string. (V14)
  225. *
  226. *   SYNOPSIS
  227. *    Index = LT_LevelWidth(Handle,FormatString,DispFunc,Min,Max,MaxWidth,
  228. *      D0                    A0        A1         A2     D0  D1    A3
  229. *
  230. *                              MaxLen,FullCheck);
  231. *                                D3      D2
  232. *
  233. *    LONG LT_LevelWidth(LayoutHandle *,STRPTR,
  234. *                       LONG (*)(struct Gadget *,WORD),LONG,LONG,LONG *,
  235. *                       LONG *,BOOL);
  236. *
  237. *   FUNCTION
  238. *    In order to make room for the level text displayed by a
  239. *    SLIDER_KIND object one needs to know how much space the
  240. *    longest level string will occupy. Otherwise, the level
  241. *    text may overwrite the gadget label text or the slider
  242. *    container. This routine will rattle through all possible
  243. *    slider settings (as given via the Min and the Max
  244. *    level values) and determine the longest label string
  245. *    according to the font used.
  246. *
  247. *   INPUTS
  248. *    Handle - Pointer to a LayoutHandle structure, as returned by
  249. *        a call to LT_CreateHandleTags().
  250. *
  251. *    FormatString - The sprintf() style formatting string to be used
  252. *        to format the slider level settings into text.
  253. *        This is the same string you would pass in via
  254. *        the GTSL_LevelFormat tag when creating the
  255. *        slider object.
  256. *        Default: "%lD" for systems which have locale.library
  257. *            installed, "%ld" otherwise.
  258. *
  259. *    DispFunc - A pointer to the function to filter the slider level
  260. *        values. The result of this function will then be
  261. *        used to format a string into the slider level text.
  262. *        This is the same parameter you would pass in via
  263. *        the GTSL_DispFunc tag when creating the slider
  264. *        object.
  265. *
  266. *        NOTE: the routine will be called with a NULL Gadget
  267. *            parameter, make sure your code will handle
  268. *            this nicely.
  269. *
  270. *        Default: no display function
  271. *
  272. *    Min - The smallest value the slider can be set to. This is
  273. *        same value you would pass in via GTSL_Min when creating
  274. *        the slider object.
  275. *
  276. *    Max - The largest value the slider can be set to. This is
  277. *        same value you would pass in via GTSL_Max when creating
  278. *        the slider object.
  279. *
  280. *    MaxWidth - Pointer to a place to store the width of the
  281. *        longest level string in pixels. If you pass
  282. *        in NULL instead of the address of a variable
  283. *        no harm will be done.
  284. *
  285. *    MaxLen - Pointer to a place to store the length of the
  286. *        longest level string in characters. If you pass
  287. *        in NULL instead of the address of a variable
  288. *        no harm will be done.
  289. *
  290. *    FullCheck - TRUE will cause the code to rattle through all
  291. *        possible slider settings, starting from the
  292. *        minimum value, ending at the maximum value.
  293. *        While this may be a good idea for a display
  294. *        function to map slider levels to text strings
  295. *        of varying length it might be a problem when
  296. *        it comes to display a range of numbers from
  297. *        1 to 40,000: the code will loop through
  298. *        40,000 iterations trying to find the longest
  299. *        string.
  300. *
  301. *        FALSE will cause the code to calculate the
  302. *        longest level string based only on the
  303. *        minimum and the maximum value to check.
  304. *        While this is certainly a good a idea when
  305. *        it comes to display a range of numbers from
  306. *        1 to 40,000 as only two values will be
  307. *        checked the code may fail to produce
  308. *        accurate results for sliders using display
  309. *        functions mapping slider levels to strings.
  310. *
  311. *   RESULT
  312. *    Index - The slider level which gives the longest
  313. *        level string.
  314. *
  315. *   NOTES
  316. *    This function does exactly what gtlayout.library/LT_LevelWidth
  317. *    does, but uses a slightly different register ordering. Namely,
  318. *    the MaxLen pointer is passed in D3 instead of A5.
  319. *
  320. *   SEE ALSO
  321. *    gtlayout.library/LT_LevelWidth
  322. *
  323. ******************************************************************************
  324. *
  325. */
  326.  
  327. VOID LIBENT
  328. LT_NewLevelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR levelFormat,REG(a2) DISPFUNC dispFunc,REG(d0) LONG min,REG(d1) LONG max,REG(a3) LONG *maxWidth,REG(d3) LONG *maxLen,REG(d2) BOOL fullCheck)
  329. {
  330.     LTP_LevelWidth(handle,levelFormat,dispFunc,min,max,maxWidth,maxLen,fullCheck);
  331. }
  332.